package stud.mpgame.physics;

/**
 * ...
 * @author pavel[f.pavel91@gmail.com]
 */
public class Vector2D {

	public float x;
	public float y;
	
	public Vector2D(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public void set(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public float dot(Vector2D v) {
		return x * v.x + y * v.y;
	}

	public float cross(Vector2D v) {
		return x * v.y - y * v.x;
	}

	public void addDot(float dx, float dy) {
		this.x += dx;
		this.y += dy;
	}

    public void add(Vector2D v) {
        this.x += v.x;
        this.y += v.y;
    }

	public Vector2D addNew(Vector2D v) {
		return new Vector2D(x + v.x, y + v.y);
	}

	public Vector2D sub(Vector2D v) {
		return new Vector2D(x - v.x, y - v.y);
	}

	public Vector2D mul(float s) {
		return new Vector2D(x * s, y * s);
	}

	public float length() {
		return (float) Math.sqrt(x * x + y * y);
	}

	public String toString() {
		return "("+(Math.round(x*100)/100)+","+(Math.round(y*100)/100)+")";
	}

	public static Vector2D normal(float x, float y) {
		float d = (float) Math.sqrt(x * x + y * y);
		float k = ( d < 1e-99 ) ? 0 : 1 / d;
		return new Vector2D( -y * k , x * k );
	}
	
	public Vector2D projection(Vector2D v) {
		float scalar = dot(v);
		float squareLength = v.x * v.x + v.y * v.y;
		float projX = ( scalar / squareLength ) * v.x;
		float projY = ( scalar / squareLength ) * v.y;
		return new Vector2D(projX, projY);
	}
	
}